package com.gopay.bis.ofpay.operation;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gopay.bis.ofpay.operation.transformer.TransformerQuery;
import com.gopay.common.constants.bank.tpas.TpasMwaChargeConstants;
import com.gopay.common.domain.bank.tpas.TpasMessage;
import com.gopay.common.domain.bank.tpas.mwa.MobileMessage;
import com.gopay.common.domain.bank.tpas.mwa.OilCardMessage;
import com.gopay.common.util.HttpTransport;
import com.gopay.common.util.MD5Util;

public class OfpayQueryHelper {
    private final static Logger logger = LoggerFactory.getLogger(OfpayQueryHelper.class);

    private HttpTransport httpTransport;
    private TransformerQuery transformer;
    
    public TpasMessage mobInfo(TpasMessage base) throws Exception {
        logger.info("ofpay query mobi info:" + ToStringBuilder.reflectionToString(base, ToStringStyle.DEFAULT_STYLE));
        String mobilenum = base.getOuterCustID().substring(0, 7);
        Map<String, String> param = new HashMap<String, String>();
        param.put("mobilenum", mobilenum);
        httpTransport.setUrl(base.getPayUrl());
        MobileMessage res = new MobileMessage();
        String resStr = "";
		try {
			resStr = httpTransport.submit(param);
		} catch (Exception e) {
			logger.error(String.format("query mobile info error, mobile=%s,res=%s", mobilenum,resStr));
            res.setRetCode(TpasMwaChargeConstants.QUERY_FAIL);
            res.setRetMsg("查询失败");
            return res;
		}
//        String resStr = "1851179|北京北京|联通";
        
        if(StringUtils.isEmpty(resStr) || resStr.split("\\|").length < 3){
            logger.error(String.format("query mobile info error, mobile=%s,res=%s", mobilenum,resStr));
            res.setRetCode(TpasMwaChargeConstants.QUERY_FAIL);
            res.setRetMsg("查询失败");
            return res;
        }
        String[] info = resStr.split("\\|");
        res.setMobileBelong(info[1]);
        res.setMobileOperator(info[2]);
        res.setRetCode(TpasMwaChargeConstants.QUERY_SUCCESS);
        res.setRetMsg("查询成功");
        return res;

    }

    public TpasMessage telCheck(TpasMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

    public TpasMessage oilCardInfo(TpasMessage base) throws Exception {
        logger.info("ofpay oilCard info req:" + ToStringBuilder.reflectionToString(base, ToStringStyle.DEFAULT_STYLE));
        OilCardMessage reqMsg = (OilCardMessage)base;
        Map<String, String> param = new HashMap<String, String>();
        param.put("userid", reqMsg.getMerID());
        param.put("userpws", reqMsg.getPrivatePwd());
        param.put("game_userid", reqMsg.getOuterCustID());
        param.put("chargeType", reqMsg.getCardType());
        param.put("md5_str", reqMsg.getPlainMessage().toUpperCase());
        param.put("version", reqMsg.getVersion());
System.out.println(param);
        httpTransport.setUrl(reqMsg.getPayUrl());
        String resXml = httpTransport.submit(param);
//        String resXml = "<?xml version=\"1.0\" encoding=\"GB2312\" ?><queryInfo><err_msg></err_msg><retcode>1</retcode>"
//        +"<game_userid>1000113200013667017</game_userid><username>陈*白</username></queryInfo> ";
        OilCardMessage result = transformer.parseOilCardInfo(resXml);
        return result;
    }

    public int queryChargeStatus(TpasMessage base) throws Exception {
        logger.info("ofpay query charge statis req:" + ToStringBuilder.reflectionToString(base, ToStringStyle.DEFAULT_STYLE));
        Map<String, String> param = new HashMap<String, String>();
        param.put("userid", base.getMerID());
        param.put("spbillid", base.getBankOrderId());
System.out.println(param);
        httpTransport.setUrl(base.getPayUrl());
        String resStr = httpTransport.submit(param);
//        String resStr = "1";
        if(StringUtils.isEmpty(resStr)){
            logger.error(String.format("query charge status error, bankorderid=%s", base.getBankOrderId()));
        }
        return Integer.parseInt(resStr);
    }

    public void setHttpTransport(HttpTransport httpTransport) {
        this.httpTransport = httpTransport;
    }

    public void setTransformer(TransformerQuery transformer) {
        this.transformer = transformer;
    }
//    public static void main(String[] args) {
//        String sign = MD5Util.sign("guofubao123");
//        userid+userpws+cardid+cardnum+sporder_id+sporder_time+ game_userid
//        md5_str=2677c8373f28bcc28132b6b423a51714, userpws=4036c537a09371d332a01cb460b712ae, 
//                game_userid=13522441895, sporder_time=20151029144344, cardnum=1, userid=A1204538,
//                sporder_id=20151028000000, mctype=, cardid=140101, ret_url=, version=6.0
//        String pack = "A12045384036c537a09371d332a01cb460b712ae1401011201510280000002015102914434413522441895";
//        String sign = MD5Util.sign(pack + "OFCARD");
//        System.out.println(sign);
//    }
}
